import {createRouter, createWebHistory} from 'vue-router'
import {useUserInfoStore} from '@/stores/User/UserInfo'
import {ElMessage} from 'element-plus'
import {getUserById} from "@/api/User";


const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/test',
            component: () => import('@/components/login/Login.vue')
        },
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            name: 'LoginView',
            component: () => import('@/components/login/Login.vue')
        },
        {
            path: '/register',
            name: 'RegisterView',
            component: () => import('@/views/RegisterView.vue')
        },
        {
            path: '/home',
            name: 'Home',
            component: () => import('@/views/HomeView.vue'),
            children: [
                {
                    path: '',
                    component: () => import('@/components/home/body/MainBody.vue')
                },
                {
                    path: 'userinfo',
                    component: () => import('@/components/home/body/userInfo/userInfo.vue')
                },
                {
                    path: 'mainbody',
                    component: () => import('@/components/home/body/MainBody.vue')
                },
                {
                    path: 'selectperson',
                    component: () => import('@/components/home/body/person/select/PersonSelect.vue')
                },
                {
                    path: 'programmanagement',
                    children: [{
                        path: '',
                        component: () => import('@/components/home/body/projectManagement/PM.vue')
                    },
                        {
                            path: 'newconfig',
                            children: [{
                                path: '',
                                component: () => import('@/components/home/body/projectManagement/NewConfig/First/ConfigFormFirst.vue')
                            },
                                {
                                    path: '1',
                                    component: () => import('@/components/home/body/projectManagement/NewConfig/First/ConfigFormFirst.vue')
                                },
                                {
                                    path: '2',
                                    component: () => import('@/components/home/body/projectManagement/NewConfig/Second/ConfigFormSecond.vue')
                                },
                                {
                                    path: '3',
                                    component: () => import('@/components/home/body/projectManagement/NewConfig/Third/ConfigFormThird.vue')
                                },
                                {
                                    path: 'finally',
                                    component: () => import('@/components/home/body/projectManagement/NewConfig/Finally/ConfigFormFinally.vue')
                                }]
                        }]
                },
                {
                    path: 'dictionary',
                    component: () => import('@/components/home/body/dictionaryMaintain/DM.vue')
                }]
        },
        {
            path: '/404',
            component: () => import('@/components/login/Login.vue')
        }
    ]
})

// 挂载路由导航守卫,控制页面访问权限

router.beforeEach((to, from, next) => {
    if (parseInt(sessionStorage.getItem("time")) > new Date().getTime()) {
        sessionStorage.clear()
    }
    if (sessionStorage.getItem("token") != null) {
        getUserById(parseInt(sessionStorage.getItem("token"))).then(response => {
                useUserInfoStore().$patch({
                    departmentId: response["data"].departmentId,
                    isDelete: response["data"].isDelete,
                    isRole: response["data"].isRole,
                    levelId: response["data"].levelId,
                    positionId: response["data"].positionId,
                    projectId: response["data"].projectId,
                    statusId: response["data"].statusId,
                    userAccount: response["data"].userAccount,
                    userAvatarUrl: response["data"].userAvatarUrl,
                    userCreateTime: response["data"].userCreateTime,
                    userEmail: response["data"].userEmail,
                    userEntryTime: response["data"].userEntryTime,
                    userGender: response["data"].userGender,
                    userId: response["data"].userId,
                    userName: response["data"].userName,
                    userPassword: response["data"].userPassword,
                    userPhone: response["data"].userPhone,
                    userUpdateTime: response["data"].userUpdateTime
                })
                if (useUserInfoStore().userId == 0 && to.path !== '/login' && to.path !== '/') {
                    ElMessage.error("登陆已失效,请重新登录")
                    // 如果没有登录 先登录
                    next({
                        path: '/login'
                    })
                } else {
                    if (to.path == '/' || to.path == '/404' || to.path == '/500' || to.path == '/login') {
                        // 不加会死循环
                        next()
                    } else {
                        let isAuth: boolean
                        // 判断访问的页面是否授权
                        // var isAuth = isAuthorized(to.path);
                        if (useUserInfoStore().isRole == 0) {
                            isAuth = false
                        } else {
                            isAuth = true
                        }
                        if (!isAuth) {
                            // 如果没有授权,自动跳转到 login 页面
                            if (to.path == '/home' || to.path == '/home/userinfo' || to.path == '/home/mainbody') {
                                next()
                            } else {
                                alert("您没有权限访问该页面")
                            }
                        } else {
                            // 如果授权,继续下一步
                            next()
                        }
                    }
                }
            }
        )
    } else {
        if (useUserInfoStore().userId == 0 && to.path !== '/login' && to.path !== '/') {
            ElMessage.error("登陆已失效,请重新登录")
            // 如果没有登录 先登录
            next({
                path: '/login'
            })
        } else {
            if (to.path == '/' || to.path == '/404' || to.path == '/500' || to.path == '/login') {
                // 不加会死循环
                next()
            } else {
                let isAuth: boolean
                // 判断访问的页面是否授权
                // var isAuth = isAuthorized(to.path);
                if (useUserInfoStore().isRole == 0) {
                    isAuth = false
                } else {
                    isAuth = true
                }
                if (!isAuth) {
                    // 如果没有授权,自动跳转到 login 页面
                    if (to.path == '/home' || to.path == '/home/userinfo' || to.path == '/home/mainbody') {
                        next()
                    } else {
                        alert("您没有权限访问该页面")
                    }
                } else {
                    // 如果授权,继续下一步
                    next()
                }
            }
        }
    }


})
export default router
